package main

import (
	"fmt"

	"hash/fnv"
	"time"

	"gitee.com/lyhmygo/go-lua/src"
	"github.com/gofrs/uuid/v5"
	lua "github.com/yuin/gopher-lua"
)

func main() {
	rt := src.GetRenewalTicker()

	go rt.ExampleNewClient("dup")
	go rt.ExampleNewClient("lup")

	time.Sleep(20 * time.Second)
	rt.DelCmd()

	// src.TickerStopCheck()
	// time.Sleep(10 * time.Second)

	m := map[uint32]int{}
	for i := 0; i < 1; i++ {
		uid, _ := uuid.NewV6()
		m[hash(uid.String())]++
	}
	for k, v := range m {
		if k >= 255 {
			fmt.Print(k, v, ";")
		}
	}
}

func hash(s string) uint32 {
	h := fnv.New32a()
	h.Write([]byte(s))
	return h.Sum32() % 256
}

func LuaTest() {
	L := lua.NewState(lua.Options{
		SkipOpenLibs: true,
		// IncludeGoStackTrace: true,
	})
	defer L.Close()

	// go
	var err error
	if err = L.DoString(`print("go to lua hello")`); err != nil {
		fmt.Println(err)
		return
	}
	if err = L.DoFile("src/main.lua"); err != nil {
		fmt.Println("L.DoFile: ", err)
		return
	}
	err = L.CallByParam(lua.P{
		Fn:      L.GetGlobal("add"), //调用的全局函数名
		NRet:    2,                  //返回参数的数量
		Protect: true,               //当为 true 时，没找到函数或者出错不会panic，只会返回 error
	}, lua.LNumber(10), lua.LNumber(3))
	if err != nil {
		fmt.Println("L.CallByParam: ", err)
	}
	// 获取值 并pop
	ret := L.Get(2)
	res, ok := ret.(lua.LNumber)
	fmt.Println(ok, res, ret)

	ret = L.Get(1)
	res, ok = ret.(lua.LNumber)
	fmt.Println(ok, res, ret)
}
